<?php

namespace App\Http\Controllers\Api;

use App\Exceptions\ApiException;
use App\Http\Requests\SubscribeRequest;
use App\Http\Resources\SubscribeCollection;
use App\model\Building;
use App\model\Land;
use App\Park;
use App\Subscribe;
use App\Workshop;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SubscribeController extends Controller
{
    public function subscribe(SubscribeRequest $subscribeRequest){
        $user=$subscribeRequest->user('api');
        $subscibe=Subscribe::where('user_id',$user->id)
            ->where('model',$subscribeRequest->model)
            ->where('model_id',$subscribeRequest->model_id)
            ->first();

        if(!$subscibe){
            $subscibe=new Subscribe();
        }
        if ($subscibe->is_subscribe==1){
            throw new ApiException('已经关注');
        }
        switch ($subscribeRequest->model){
            case Subscribe::WORKSHOP_BUY:
                $model=Workshop::find($subscribeRequest->model_id);
                break;
            case Subscribe::WORKSHOP_RENT:
                $model=Workshop::find($subscribeRequest->model_id);
                break;
            case  Subscribe::BUILD_RENT:
                $model=Building::find($subscribeRequest->model_id);
                break;
            case  Subscribe::BUILD_BUY:
                $model=Building::find($subscribeRequest->model_id);
                break;
            case  Subscribe::PARK_BUY:
                $model=\App\model\Park::find($subscribeRequest->model_id);
                break;
            case  Subscribe::PARK_RENT:
                $model=\App\model\Park::find($subscribeRequest->model_id);
                break;
            case  Subscribe::LAND_RENT:
                $model=Land::find($subscribeRequest->model_id);
                break;
            case  Subscribe::LAND_BUY:
                $model=Land::find($subscribeRequest->model_id);
                break;
            default :
                throw  new ApiException('位置错误');
        }
        $subscibe->title=$model->title;
        $subscibe->logo=$model->logo;
        $subscibe->user_id=$user->id;
        $subscibe->model=$subscribeRequest->model;
        $subscibe->model_id=$subscribeRequest->model_id;
        $subscibe->is_subscribe=1;
        $subscibe->href=$subscribeRequest->href;
        $subscibe->save();
        return \Response::json([
            'code'=>0,
            'data'=>[

            ]
        ]);
    }
    public function unSubscribe(SubscribeRequest $subscribeRequest){
        $user=$subscribeRequest->user('api');
        $subscibe=Subscribe::where('user_id',$user->id)
            ->where('model',$subscribeRequest->model)
            ->where('model_id',$subscribeRequest->model_id)
            ->where('is_subscribe',1)
            ->first();
        if(!$subscibe){
            throw  new ApiException('未关注');
        }
        $subscibe->is_subscribe=0;
        $subscibe->save();
        return response()->json(
            [
                'code'=>0,
                'data'=>'取消关注成功'
            ]
        );
    }
    public function isSubscribe(SubscribeRequest $subscribeRequest){//是否关注
        $user=$subscribeRequest->user('api');
        if($user){
            $subscibe=Subscribe::where('user_id',$user->id)
                ->where('model',$subscribeRequest->model)
                ->where('model_id',$subscribeRequest->model_id)
                ->where('is_subscribe',1)
                ->first();
            return \Response::json([
                'code'=>0,
                'data'=>[
                    'is_subscribe'=>$subscibe->is_subscribe ?? 0
                ]
            ]);
        }else{
            return \Response::json([
                'code'=>0,
                'data'=>[
                    'is_subscribe'=>0
                ]
            ]);
        }
    }
    public function SubscribeList(Request $request){
        $user=$request->user('api');
        $subscribe=Subscribe::where('user_id',$user->id)
            ->where('is_subscribe',1)
            ->orderBy('id','desc')
            ->paginate(10);
        return new SubscribeCollection($subscribe);
    }

}
